// pages/play/play.js
const audio = wx.getBackgroundAudioManager();//引入audio组件
Page({

   /**
    * 页面的初始数据
    */
   data: {
      song: [],
      isPlay: true,
      playUrl: "",
      animation: "",
      rotateCount: 0,
      animationIntervalId: null
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
      wx.showToast({
         title: '加载中',
         icon: "loading"
      })
      this.setData({
         isPlay: true,
      })
      var { id } = options;
      wx.request({
         url: 'https://music.aityp.com/song/url?id=' + id,
         data: {},
         header: { 'content-type': 'application/json' },
         method: 'GET',
         dataType: 'json',
         responseType: 'text',
         success: (res) => {
            var playUrl = res.data.data[0].url;
            this.setData({
               playUrl,
            })
         }
      });
      wx.request({
         url: 'https://music.aityp.com/song/detail?ids=' + id,
         data: {},
         header: { 'content-type': 'application/json' },
         method: 'GET',
         dataType: 'json',
         responseType: 'text',
         success: (res) => {
            wx.hideToast();
            this.setData({
               song: res.data.songs[0],
            })
         }
      });
      let animation = wx.createAnimation({
         duration: 100,
      });
      var that = this;
      that.data.animationIntervalId = setInterval(function () {
         that.setData({
            animation: animation.rotate((++that.data.rotateCount) * 2).step().export()
         });
      }, 100);

      wx.playBackgroundAudio({
         dataUrl: 'https://music.163.com/song/media/outer/url?id=' + id,
      });
   },
   handlemusic() {

      if (this.data.isPlay) {
         audio.pause();
         this.setData({
            isPlay: false
         })

         /* 图片暂停 */
         var that = this;
         if (that.data.animationIntervalId != null) {
            clearInterval(that.data.animationIntervalId);
         }
         that.setData({
            animation: ''
         });

      } else {
         audio.src = this.data.playUrl;
         audio.title = this.data.song.name;
         this.setData({
            isPlay: true
         });

         /* 图片旋转 */
         let animation = wx.createAnimation({
            duration: 100,
         });
         var that = this;

         that.data.animationIntervalId = setInterval(function () {
            that.setData({
               animation: animation.rotate((++that.data.rotateCount) * 2).step().export()
            });
         }, 100);
      }
   }



})